package com.silence.qhyh.webpro.mvp.ui.activity.login;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.silence.qhyh.webpro.R;
import com.silence.qhyh.webpro.app.widget.verificationcode.PhoneNumberTextWatcher;
import com.silence.qhyh.webpro.mvp.ui.activity.mine.PaySuccessActivity;
import com.silence.qhyh.webpro.mvp.ui.activity.mine.VipUpdateActivity;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxPermissionsTool;
import com.vondear.rxtool.RxRegTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.RxTitle;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author silence
 * 登录
 */
public class LoginActivity extends BaseActivity implements PhoneNumberTextWatcher.OnListenerLengthChange {

    @BindView(R.id.rx_title)
    RxTitle rxTitle;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.cb_agree)
    CheckBox cbAgree;
    @BindView(R.id.iv_next)
    ImageView ivNext;
    @BindView(R.id.iv_clear)
    ImageView ivClear;

    private PhoneNumberTextWatcher phoneNumberTextWatcher;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_login;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        permissions();
        rxTitle.setLeftFinish(this);
        phoneNumberTextWatcher = new PhoneNumberTextWatcher(etPhone);
        phoneNumberTextWatcher.setOnLengthListener(this);
        etPhone.addTextChangedListener(phoneNumberTextWatcher);
    }

    @OnClick({R.id.iv_next, R.id.cb_agree, R.id.iv_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_next:
//                if (!RxRegTool.isMobileSimple(etPhone.getText().toString().replaceAll(" ", ""))) {
//                    RxToast.normal(getString(R.string.app_login7));
//                    return;
//                }
//                if (!cbAgree.isChecked()) {
//                    RxToast.normal(getString(R.string.app_login8));
//                    return;
//                }
//                Bundle bundle = new Bundle();
//                bundle.putString("phone", etPhone.getText().toString().replaceAll(" ", ""));
//                RxActivityTool.skipActivity(this, VerificationCodeActivity.class, bundle);
                RxActivityTool.skipActivity(this, VipUpdateActivity.class);
                break;
            case R.id.cb_agree:
                if (etPhone.getText().toString().length() == 13 && cbAgree.isChecked()) {
                    ivNext.setImageResource(R.mipmap.icon_sel_next_sel);
                } else {
                    ivNext.setImageResource(R.mipmap.icon_sel_next_nor);
                }
                break;
            case R.id.iv_clear:
                etPhone.setText("");
                ivNext.setImageResource(R.mipmap.icon_sel_next_nor);
                break;
            default:
                break;
        }
    }

    /**
     * 获取输入长度
     * */
    @Override
    public void getLength(int length) {
        if (length > 0){
            if (length == 13 && cbAgree.isChecked()) {
                ivNext.setImageResource(R.mipmap.icon_sel_next_sel);
            } else {
                ivNext.setImageResource(R.mipmap.icon_sel_next_nor);
            }
            ivClear.setVisibility(View.VISIBLE);
        } else {
            ivClear.setVisibility(View.GONE);
        }
    }

    private void permissions() {
        RxPermissionsTool.
                with(this).
                addPermission(Manifest.permission.ACCESS_FINE_LOCATION).
                addPermission(Manifest.permission.ACCESS_COARSE_LOCATION).
                addPermission(Manifest.permission.READ_EXTERNAL_STORAGE).
                addPermission(Manifest.permission.CAMERA).
                addPermission(Manifest.permission.CALL_PHONE).
                addPermission(Manifest.permission.READ_SMS).
                addPermission(Manifest.permission.SEND_SMS).
                addPermission(Manifest.permission.RECEIVE_SMS).
                addPermission(Manifest.permission.READ_PHONE_STATE).
                addPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE).
                initPermission();
    }
}
